package com.ziytek.web.citizen.service.impl;

import com.ziytek.web.citizen.constant.NumConstant;
import com.ziytek.web.citizen.constant.ServerConstant;
import com.ziytek.web.citizen.model.bean.DataProcess;
import com.ziytek.web.citizen.model.bean.ProcessDetails;
import com.ziytek.web.citizen.service.itf.DataProcessService;
import com.ziytek.web.citizen.util.HexUtil;
import com.ziytek.web.citizen.util.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fenghx
 * @version 1.0
 * @className DataProcessServiceImpl
 * Description:
 * @date 2024/4/16 16:00
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DataProcessServiceImpl implements DataProcessService {
    @Override
    public List<DataProcess> processData(String data, String key) {
        if (StringUtil.isEmpty(data) || data.length() != NumConstant.INT_DATA_END_LEN) {
            log.error("getBatteriesDetails电芯周期数据处理失败，数据长度不对length[{}]", data.length());
            return null;
        }

        data = data.substring(NumConstant.INT_ZERO, NumConstant.INT_DATA_ACTUAL_LENGTH);
        var bordData = data.substring(NumConstant.INT_TWELVE, NumConstant.INT_DATA_END);
        List<DataProcess> dataProcessesList = new ArrayList<>();
        for (int i = NumConstant.INT_ZERO; i < bordData.length() / NumConstant.INT_EIGHTY_SIX; i++) {
            var bData = bordData.substring(i * NumConstant.INT_EIGHTY_SIX, (i + NumConstant.INT_ONE) * NumConstant.INT_EIGHTY_SIX);
            var controlAddress = bData.substring(ServerConstant.DATA_TYPE_LEN_0, ServerConstant.DATA_TYPE_LEN_1);
            var value = bData.substring(NumConstant.INT_SIX, NumConstant.INT_EIGHTY_SIX);
            if (!StringUtil.isEmpty(key) && key.equals(controlAddress)) {
                List<DataProcess> newDdataProcessesList = new ArrayList<>();
                var dataProcess = new DataProcess();
                dataProcess.setKey(controlAddress);
                dataProcess.setValue(value);
                List<ProcessDetails> detail = new ArrayList<>();
                for (int j = NumConstant.INT_ZERO; j < value.length() / ServerConstant.BATTERIES_ONE_DATA_LENGTH; j++) {
                    var battery = value.substring(j * ServerConstant.BATTERIES_ONE_DATA_LENGTH, (j + NumConstant.INT_ONE) * ServerConstant.BATTERIES_ONE_DATA_LENGTH);
                    var processDetails = new ProcessDetails();
                    processDetails.setP(battery.substring(ServerConstant.DATA_TYPE_LEN_0, ServerConstant.DATA_TYPE_LEN_1));
                    processDetails.setC(battery.substring(ServerConstant.DATA_TYPE_LEN_1, ServerConstant.DATA_TYPE_LEN_END));
                    processDetails.setV(battery.substring(ServerConstant.DATA_TYPE_LEN_END, ServerConstant.BATTERIES_ONE_DATA_LENGTH));
                    processDetails.setC(String.valueOf(HexUtil.hexToInt(processDetails.getC())));
                    processDetails.setV(String.valueOf(HexUtil.hexToInt(processDetails.getV())));
                    detail.add(processDetails);
                }
                dataProcess.setDetail(detail);
                newDdataProcessesList.add(dataProcess);
                return newDdataProcessesList;
            }
            var dataProcess = new DataProcess();
            dataProcess.setKey(controlAddress);
            dataProcess.setValue(value);
            List<ProcessDetails> detail = new ArrayList<>();
            for (int j = NumConstant.INT_ZERO; j < value.length() / ServerConstant.BATTERIES_ONE_DATA_LENGTH; j++) {
                String battery = value.substring(j * ServerConstant.BATTERIES_ONE_DATA_LENGTH, (j + NumConstant.INT_ONE) * ServerConstant.BATTERIES_ONE_DATA_LENGTH);
                ProcessDetails processDetails = new ProcessDetails();
                processDetails.setP(battery.substring(ServerConstant.DATA_TYPE_LEN_0, ServerConstant.DATA_TYPE_LEN_1));
                processDetails.setC(battery.substring(ServerConstant.DATA_TYPE_LEN_1, ServerConstant.DATA_TYPE_LEN_END));
                processDetails.setV(battery.substring(ServerConstant.DATA_TYPE_LEN_END, ServerConstant.BATTERIES_ONE_DATA_LENGTH));
                processDetails.setC(String.valueOf(HexUtil.hexToInt(processDetails.getC())));
                processDetails.setV(String.valueOf(HexUtil.hexToInt(processDetails.getV())));
                detail.add(processDetails);
            }
            dataProcess.setDetail(detail);
            dataProcessesList.add(dataProcess);
        }
        return dataProcessesList;
    }

    @Override
    public List<DataProcess> processCapacityData(String data, String key) {
        if (StringUtil.isEmpty(data) || data.length() != NumConstant.INT_DATA_END_LEN) {
            log.error("processCapacityData电芯分容结果数据处理失败，数据长度不对length[{}]", data.length());
            return null;
        }

        data = data.substring(NumConstant.INT_ZERO, NumConstant.INT_DATA_ACTUAL_LENGTH);
        String bordData = data.substring(NumConstant.INT_TWELVE, NumConstant.INT_DATA_END);
        List<DataProcess> dataProcessesList = new ArrayList<>();
        for (int i = NumConstant.INT_ZERO; i < bordData.length() / NumConstant.INT_EIGHTY_SIX; i++) {
            String bData = bordData.substring(i * NumConstant.INT_EIGHTY_SIX, (i + NumConstant.INT_ONE) * NumConstant.INT_EIGHTY_SIX);
            String controlAddress = bData.substring(ServerConstant.DATA_TYPE_LEN_0, ServerConstant.DATA_TYPE_LEN_1);
            String value = bData.substring(NumConstant.INT_SIX, NumConstant.INT_EIGHTY_SIX);
            if (!StringUtil.isEmpty(key) && key.equals(controlAddress)) {
                List<DataProcess> newDdataProcessesList = new ArrayList<>();
                DataProcess dataProcess = new DataProcess();
                dataProcess.setKey(controlAddress);
                dataProcess.setValue(value);
                List<ProcessDetails> detail = new ArrayList<>();
                for (int j = NumConstant.INT_ZERO; j < value.length() / ServerConstant.BATTERIES_ONE_DATA_LENGTH; j++) {
                    String battery = value.substring(j * ServerConstant.BATTERIES_ONE_DATA_LENGTH, (j + NumConstant.INT_ONE) * ServerConstant.BATTERIES_ONE_DATA_LENGTH);
                    ProcessDetails processDetails = new ProcessDetails();
                    processDetails.setP(battery.substring(ServerConstant.DATA_TYPE_LEN_0, ServerConstant.DATA_TYPE_LEN_1));
                    //processDetails.setC(battery.substring(ServerConstant.DATA_TYPE_LEN_1, ServerConstant.DATA_TYPE_LEN_END));
                    //processDetails.setV(battery.substring(ServerConstant.DATA_TYPE_LEN_END, ServerConstant.BATTERIES_ONE_DATA_LENGTH));
                    //processDetails.setC(String.valueOf(HexUtil.hexToInt(processDetails.getC())));
                    processDetails.setV("0");

                    String capacity = battery.substring(NumConstant.INT_EIGHT, NumConstant.INT_TEN)
                            + battery.substring(NumConstant.INT_SIX, NumConstant.INT_EIGHT)
                            + battery.substring(NumConstant.INT_FOUR, NumConstant.INT_SIX)
                            + battery.substring(NumConstant.INT_TWO, NumConstant.INT_FOUR);
                    float capacityNum = NumConstant.FLOAT_ZER;
                    try {
                        capacityNum = HexUtil.hexStrToFloat(capacity);
                    } catch (Exception ignored) {
                        // 容量数据异常,视为0
                    }
                    processDetails.setC(String.valueOf(capacityNum));
                    detail.add(processDetails);
                }
                dataProcess.setDetail(detail);
                newDdataProcessesList.add(dataProcess);
                return newDdataProcessesList;
            }
            DataProcess dataProcess = new DataProcess();
            dataProcess.setKey(controlAddress);
            dataProcess.setValue(value);
            List<ProcessDetails> detail = new ArrayList<>();
            for (int j = NumConstant.INT_ZERO; j < value.length() / ServerConstant.BATTERIES_ONE_DATA_LENGTH; j++) {
                String battery = value.substring(j * ServerConstant.BATTERIES_ONE_DATA_LENGTH, (j + NumConstant.INT_ONE) * ServerConstant.BATTERIES_ONE_DATA_LENGTH);
                ProcessDetails processDetails = new ProcessDetails();
                processDetails.setP(battery.substring(ServerConstant.DATA_TYPE_LEN_0, ServerConstant.DATA_TYPE_LEN_1));
                // processDetails.setC(battery.substring(ServerConstant.DATA_TYPE_LEN_1, ServerConstant.DATA_TYPE_LEN_END));
                // processDetails.setV(battery.substring(ServerConstant.DATA_TYPE_LEN_END, ServerConstant.BATTERIES_ONE_DATA_LENGTH));
                // processDetails.setC(String.valueOf(HexUtil.hexToInt(processDetails.getC())));
                // processDetails.setV(String.valueOf(HexUtil.hexToInt(processDetails.getV())));

                processDetails.setV("0");

                String capacity = battery.substring(NumConstant.INT_EIGHT, NumConstant.INT_TEN)
                        + battery.substring(NumConstant.INT_SIX, NumConstant.INT_EIGHT)
                        + battery.substring(NumConstant.INT_FOUR, NumConstant.INT_SIX)
                        + battery.substring(NumConstant.INT_TWO, NumConstant.INT_FOUR);
                float capacityNum = NumConstant.FLOAT_ZER;
                try {
                    capacityNum = HexUtil.hexStrToFloat(capacity);
                } catch (Exception ignored) {
                    // 容量数据异常,视为0
                }
                processDetails.setC(String.valueOf(capacityNum));

                detail.add(processDetails);
            }
            dataProcess.setDetail(detail);
            dataProcessesList.add(dataProcess);
        }
        return dataProcessesList;
    }
}
